package demo;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class SomeServiceImpl implements SomeService{
	
	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public User findUser() {
		
		String sql = "select * from user where id = ?";
		Object[] obj = {1};
		User user = jdbcTemplate.queryForObject(sql, obj, new BeanPropertyRowMapper<>(User.class));
		return user;
	}

	@Override
	public Collection<User> findAllUsers() {
		
		String sql = "select * from user";
		List<User> userList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
		return userList;
	}

	@Override
	public void addUser(User user) {
		
//		jdbcTemplate.update("insert into user (name, phone) values (?,?) ", new Object[]{"xiaoli", "1234"});
		jdbcTemplate.update("insert into user (name, phone) values (?,?) ", new Object[]{user.getName(),user.getPhone()});
		
	}

}
